package cn.com.customer.manager.utils.phone;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.XHttpProxy;
import com.xuexiang.xhttp2.annotation.ThreadType;
import com.xuexiang.xhttp2.model.ApiResult;

import java.util.HashMap;
import java.util.Map;

import cn.com.customer.manager.bean.CustomerInfo;
import cn.com.customer.manager.bean.RequestBean;
import cn.com.customer.manager.http.BaseApi;
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber;
import cn.com.customer.manager.wigeit.floatview.AppSwitchView;
import cn.com.customer.manager.wigeit.floatview.service.AppMonitorService;

public class PhoneService extends Service {
    CallModel callModel;
    private AppSwitchView mAppSwitchView;

    public PhoneService() {
    }


    @Override
    public IBinder onBind(Intent intent) {

        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startCallListener();
    }

    /*
     * 开启服务
     * */
    private void startCallListener() {
        Log.e("phone", "startCallListener");
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            TelecomManager telecomManager = (TelecomManager) this.getSystemService(Context.TELECOM_SERVICE);
        }
        callModel = new CallModel((TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE)) {
            @SuppressLint("CheckResult")
            @Override
            protected void onStateRinging(int state, String phoneNumber) {
                Map<String,String> parameters=new HashMap<>();
                parameters.put("customerMobile",phoneNumber);
                XHttp.custom().call(XHttp.custom().create(BaseApi.ICommunication.class).queryByMobile(new RequestBean(parameters))).subscribeWith(new TipRequestSubscriber<ApiResult<CustomerInfo>>() {

                    @Override
                    protected void onSuccess(ApiResult<CustomerInfo> s) {
                        init(s.getData());

                    }
                });
            }

            @Override
            protected void onStateOffhook(int state, String phoneNumber) {

            }

            @Override
            protected void onStateIdle(int state, String phoneNumber) {
              dissmiss();
            }
        };

    }

    private void init(CustomerInfo info) {
        if (mAppSwitchView == null) {
            mAppSwitchView = new AppSwitchView(this);
            mAppSwitchView.show();
        }
        mAppSwitchView.updateAppInfo(info);

    }
  private void dissmiss(){
      if (mAppSwitchView != null) {
          mAppSwitchView.clear();
          mAppSwitchView = null;
      }
  }
    /*
     * 取消监听
     * */
    public void listenNone() {
        callModel.listenNone();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        listenNone();
    }
}
